package com.kindness.frate.activity;

import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.kindness.frate.R;
import com.kindness.frate.pull.PullRefreshListener;
import com.kindness.frate.pull.PullRefreshRecyclerView;
import com.kindnesslibrary.adapter.CommonAdapter;
import com.kindnesslibrary.adapter.ViewHolder;
import com.kindnesslibrary.base.BaseActivity;
import com.kindnesslibrary.util.EnableLinearManager;
import com.kindnesslibrary.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 模仿下拉刷新
 */
public class PullRefreshActivity extends BaseActivity {

    PullRefreshRecyclerView prrv_refresh;
    List<String> datas = new ArrayList<>();
    CommonAdapter commonAdapter;

    @Override
    protected void handlerMessage(Message msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.activity_pull_refresh_layout;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        prrv_refresh = getView(R.id.prrv_refresh);
        prrv_refresh.setLayoutManager(new EnableLinearManager(this, LinearLayoutManager.VERTICAL, false));
        commonAdapter = new CommonAdapter<String>(this, R.layout.item_pull_refresh_layout, datas){

            @Override
            public void convert(ViewHolder holder, String s) {

            }
        };
        prrv_refresh.setAdapter(commonAdapter);
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        datas.add("1111");
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    protected void initEvent() {
        prrv_refresh.setPullRefreshListener(new PullRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(() -> {
                    prrv_refresh.refreshComplete();
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                handler.postDelayed(() -> {
                    ToastUtil.showToast("加载完成");
                    prrv_refresh.loadComplete();
                }, 2000);
            }
        });
    }
}
